<template>
  <div>
    <div class="years" ref="years"></div>
  </div>
</template>
<script>
import * as echarts from "echarts";
export default {
  data() {
    return {};
  },
  mounted() {
    // 绘制企业年限 图
    this.drawyear();
  },
  methods: {
    // 绘制企业发展 图
    drawyear() {
      // 1.初始化绘图容器
      let years = echarts.init(this.$refs.years);
      // 2.绘图配置项
      let option2 = {
        tooltip: {
          trigger: "axis",
        },
        grid: {
          left: "1%",
          right: "3%",
          top: "5%",
          bottom: "2%",
          containLabel: true,
        },
        xAxis: {
          show: false,
        },
        yAxis: {
          type: "category",
          data: ["瞪羚企业", "上市公司", "骨干企业", "高新技术企业"],
          nameLocation: "center",
          axisLabel: {
            color: "#333",
            fontFamily: "Microsoft YaHei",
            fontWeight: 400,
            fontSize: 16,
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },

        series: [
          {
            type: "bar",
            barWidth: "50%",
            data: [157, 25, 30, 308],
            label: {
              show: true,
              position: "right",
              formatter: "{c}户",
              color: "#333",
              fontFamily: "Microsoft YaHei",
              fontWeight: 400,
              fontSize: 16,
            },
            itemStyle: {
              normal: {
                color: {
                  type: "linear",
                  x: 1,
                  y: 0,
                  x2: 0,
                  y2: 0,
                  colorStops: [
                    {
                      offset: 0,
                      color: "#A36CFF", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "#33B8E4", // 100% 处的颜色
                    },
                  ],
                  global: false, // 缺省为 false
                },
              },
              emphasis: {
                color: "rgba(70,237,238)",
              },
            },
          },
        ],
      };
      // 3,绘图
      years.setOption(option2);
      // 自动高亮动画
      var myChartPieIndex = 0;
      var a = setInterval(function () {
        var dataLen = option2.series[0].data.length;
        // 取消之前高亮的图形
        years.dispatchAction({
          type: "downplay",
          seriesIndex: 0,
          dataIndex: myChartPieIndex,
        });
        myChartPieIndex = (myChartPieIndex + 1) % dataLen;
        // 高亮当前图形
        years.dispatchAction({
          type: "highlight",
          seriesIndex: 0,
          dataIndex: myChartPieIndex,
        });
      }, 800);
    },
  },
};
</script>

<style scoped>
.years {
  width: 100%;
  height: 300px;
}
</style>